<template>
  <div class="register">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <span>用户注册</span>
        </div>
      </template>

      <el-form 
        :model="registerForm" 
        :rules="rules"
        ref="formRef"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="registerForm.password" 
            type="password" 
            show-password
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            type="password" 
            show-password
          />
        </el-form-item>

        <el-form-item label="注册角色" prop="role">
          <el-select v-model="registerForm.role" placeholder="请选择角色">
            <el-option label="客户" value="customer" />
            <el-option label="采购员" value="purchaser" />
            <el-option label="销售员" value="salesman" />
            <el-option label="配送员" value="deliveryman" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleRegister">注册</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const formRef = ref(null)

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  role: ''
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (registerForm.confirmPassword !== '') {
      formRef.value?.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择注册角色', trigger: 'change' }
  ]
}

async function handleRegister() {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    const { confirmPassword, ...registerData } = registerForm
    await axios.post('/api/users/register/', registerData)
    ElMessage.success('注册成功')
    router.push('/login')
  } catch (error) {
    if (error.name === 'ValidationError') {
      ElMessage.error('请检查表单填写是否正确')
    } else {
      console.error('注册失败:', error)
      ElMessage.error('注册失败')
    }
  }
}

function handleReset() {
  formRef.value?.resetFields()
}
</script>

<style scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.register-card {
  width: 100%;
  max-width: 600px;
}

.card-header {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
}

.el-form {
  margin-top: 20px;
}
</style> 